//
//  SearchViewController.m
//  A段项目
//
//  Created by dlios on 15/7/14.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SearchViewController.h"
#import "BaseHeader.h"
#import "SANetWorkingTask.h"
#import "RecSecViewController.h"


@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *showData;
// 用来存放搜索显示的数据
@property (nonatomic, retain) NSMutableArray *dataList;
// 用来存放原始数据
@property (nonatomic, retain) UISearchBar *search;

@end

@implementation SearchViewController

- (void)dealloc
{
    [_showData release];
    [_dataList release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.showData = [NSMutableArray array];
        self.dataList = [NSMutableArray array];
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self searchBar];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    // Do any additional setup after loading the view.
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)searchBar
{
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 5, ScreenWidth - 10, 50)];
    [self.view addSubview:_search];
    [_search release];
    _search.searchBarStyle = 2;
    _search.delegate = self;

}

#pragma mark -
#pragma mark  searchBar的协议
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{
    if (searchText!=nil && searchText.length>0) {
        self.showData= [NSMutableArray array];
        for (NSString *tempStr in _dataList) {
            if ([tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch].length >0 ) {
                [_showData addObject:tempStr];
            }
        }
        [_table reloadData];
    }
    else
    {
        self.showData = [NSMutableArray arrayWithArray:_dataList];
        [_table reloadData];
    }
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    [self searchBar:self.search textDidChange:nil];
    [_search resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self searchBar:self.search textDidChange:nil];
    [_search resignFirstResponder];

    UIImageView *load = [[UIImageView alloc] initWithFrame:CGRectMake(0, _search.frame.origin.y + 50, ScreenWidth, ScreenHeidth - 100)];
    load.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:load];
    [load release];
    // 字符串 拼接
    NSString *url = [NSString stringWithFormat:@"http://59.151.102.96/sugforapp.php?k=%@&en=utf-8", searchBar.text];
    [SANetWorkingTask request:url parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"sugforapp"];
        [_showData addObjectsFromArray:arr];
        self.table = [[UITableView alloc] initWithFrame:CGRectMake(10, searchBar.frame.origin.y + 50, ScreenWidth - 20, ScreenHeidth - 100)];
        [self.view addSubview:_table];
        [_table release];
        _table.delegate = self;
        _table.dataSource = self;
        [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"a"];
    }];
    
}


#pragma mark -
#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_showData count]>0?[_showData count]:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    cell.textLabel.text = [[_showData objectAtIndex:indexPath.row] objectForKey:@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecSecViewController *enter = [[RecSecViewController alloc] init];
    NSString *str = [[_showData objectAtIndex:indexPath.row] objectForKey:@"id"];
    str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/car/GetSerialInfo?csid=%@&tracker=172_ycapp", str];
    [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        enter.str = [dic objectForKey:@"shareUrl"];
        [self.navigationController pushViewController:enter animated:YES];
        [enter release];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
